package cn.lh.user.controller;

import cn.lh.basic.util.JsonResult;

import cn.lh.org.exception.BusinessException;
import cn.lh.user.dto.BinderDto;
import cn.lh.user.dto.LoginDto;
import cn.lh.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public JsonResult accountLogin(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = logininfoService.accountLogin(loginDto);
            return JsonResult.set(true, "登录成功").setResultObject(map);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, e.getMessage());
        }
    }

    @GetMapping("/wechat/{code}")
    public JsonResult wechatLogin(@PathVariable("code") String code) {
        try {
            return logininfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, e.getMessage());
        }
    }

    @PostMapping("/wechat/binder")
    public JsonResult wechatBinder(@RequestBody BinderDto binderDto) {
        try {
            return logininfoService.wechatBinder(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.set(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "系统异常，稍后重试");
        }
    }

    @GetMapping("/logout/{token}")
    public JsonResult logout(@PathVariable("token") String token) {
        try {
            return logininfoService.logout(token);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.set(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "系统异常，稍后重试");
        }
    }
}
